//
//  SuggestionViewController.m
//  SPDBank
//
//  Created by hanxifeng on 14-12-25.
//  Copyright (c) 2014年 王 焕. All rights reserved.
//

#import "SuggestionViewController.h"

@interface SuggestionViewController ()

@end

@implementation SuggestionViewController{
    UITextField *_suggestionField;//占位意见
    UITextField *userName;//用户名
    UITextField *userTel;//电话
    UITextView *suggestionText;//意见
    UIAlertView *_alertView;//弹出信息
    NSInteger type;//意见类型
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"浦发银行"];
    //背景图
    self.view.backgroundColor = [UIColor colorWithPatternImage:IMG(@"suggestionBack")];
    //搭建界面
    [self buildUI];
}

#pragma mark 搭建界面
-(void)buildUI{
    CGFloat padding = 120;//白板距离两边的距离
    CGFloat paddingTop = 40;//距离上一个控件的距离
    CGFloat paddingWhite = 60;//控件距离白板两边的距离
    UIView *whiteBackView = [[UIView alloc]initWithFrame:CGRectMake(padding,10, WIDTH - 2*padding, HEIGH - 44 - 20 - 40)];
    whiteBackView.backgroundColor = [UIColor colorWithPatternImage:IMG(@"whiteBack")];
    [self.view addSubview:whiteBackView];
    
    //用户名
    userName = [[UITextField alloc]initWithFrame:CGRectMake(paddingWhite, paddingTop, whiteBackView.frame.size.width - 2*paddingWhite, 40)];
    userName.leftView = [[UIImageView alloc]initWithImage:IMG(@"account")];
    userName.leftViewMode = UITextFieldViewModeAlways;
    userName.placeholder = @" 用户名";
    userName.backgroundColor = [UIColor colorWithPatternImage:IMG(@"textField")];
    [whiteBackView addSubview:userName];
    
    //横线1
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(userName.frame)+paddingTop/2, whiteBackView.frame.size.width - 10, 1)];
    line1.backgroundColor = [UIColor blackColor];
    [whiteBackView addSubview:line1];
    
    //电话
    userTel = [[UITextField alloc]initWithFrame:CGRectMake(paddingWhite, CGRectGetMaxY(userName.frame)+paddingTop, whiteBackView.frame.size.width - 2*paddingWhite, 40)];
    userTel.keyboardType = UIKeyboardTypeNumberPad;
    userTel.placeholder = @" 电话";
    userTel.backgroundColor = [UIColor colorWithPatternImage:IMG(@"textField")];
    userTel.leftView = [[UIImageView alloc]initWithImage:IMG(@"tel")];
    userTel.leftViewMode = UITextFieldViewModeAlways;
    [whiteBackView addSubview:userTel];
    
    //横线2
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(userTel.frame)+paddingTop/2, whiteBackView.frame.size.width - 10, 1)];
    line2.backgroundColor = [UIColor blackColor];
    [whiteBackView addSubview:line2];
    
    //意见类别 view
    UIView *btnBackView = [[UIView alloc]initWithFrame:CGRectMake(paddingWhite,CGRectGetMaxY(userTel.frame)+paddingTop, whiteBackView.frame.size.width - 2*paddingWhite, 40)];
    btnBackView.backgroundColor = [UIColor colorWithPatternImage:IMG(@"textField")];
    [whiteBackView addSubview:btnBackView];
    //意见，表扬，投诉 按钮
    CGFloat btnWidth = 150;
    CGFloat btnHeight = 30;
    CGFloat paddingBtn = (whiteBackView.frame.size.width - 2*paddingWhite - 3*btnWidth)/2;
    UIButton *opinionBtn = [[UIButton alloc]initWithFrame:CGRectMake(paddingWhite, CGRectGetMaxY(line2.frame)+paddingTop/2+5, btnWidth, btnHeight)];
    opinionBtn.tag = 101;
    [opinionBtn setTitle:@"  投诉" forState:UIControlStateNormal];
    [opinionBtn setTitleColor:RGB(165, 164, 165) forState:UIControlStateNormal];
    [opinionBtn setImage:IMG(@"unselected") forState:UIControlStateNormal];
    [opinionBtn setImage:IMG(@"selected") forState:UIControlStateSelected];
    [whiteBackView addSubview:opinionBtn];
    //表扬
    UIButton *praiseBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(opinionBtn.frame)+paddingBtn, CGRectGetMaxY(line2.frame)+paddingTop/2+5, btnWidth, btnHeight)];
    praiseBtn.tag = 102;
    [praiseBtn setTitle:@"  建议" forState:UIControlStateNormal];
    [praiseBtn setTitleColor:RGB(165, 164, 165) forState:UIControlStateNormal];
    [praiseBtn setImage:IMG(@"unselected") forState:UIControlStateNormal];
    [praiseBtn setImage:IMG(@"selected") forState:UIControlStateSelected];
    [whiteBackView addSubview:praiseBtn];
    //投诉
    UIButton *complaintBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(praiseBtn.frame)+paddingBtn, CGRectGetMaxY(line2.frame)+paddingTop/2+5, btnWidth, btnHeight)];
    complaintBtn.tag = 103;
    [complaintBtn setTitle:@"  投诉" forState:UIControlStateNormal];
    [complaintBtn setTitleColor:RGB(165, 164, 165) forState:UIControlStateNormal];
    [complaintBtn setImage:IMG(@"unselected") forState:UIControlStateNormal];
    [complaintBtn setImage:IMG(@"selected") forState:UIControlStateSelected];
    [whiteBackView addSubview:complaintBtn];
    //意见类别的点击事件
    [opinionBtn addTarget:self action:@selector(tagAct:) forControlEvents:UIControlEventTouchUpInside];
    [praiseBtn addTarget:self action:@selector(tagAct:) forControlEvents:UIControlEventTouchUpInside];
    [complaintBtn addTarget:self action:@selector(tagAct:) forControlEvents:UIControlEventTouchUpInside];
    //默认选中意见类型
    [self tagAct:opinionBtn];
    
    //横线3
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(opinionBtn.frame)+paddingTop/2, whiteBackView.frame.size.width - 10, 1)];
    line3.backgroundColor = [UIColor blackColor];
    [whiteBackView addSubview:line3];
    
    //意见
    suggestionText = [[UITextView alloc]initWithFrame:CGRectMake(paddingWhite, CGRectGetMaxY(line3.frame)+paddingTop/2,whiteBackView.frame.size.width - 2*paddingWhite, 300)];
    suggestionText.delegate = self;
    suggestionText.backgroundColor = [UIColor colorWithPatternImage:IMG(@"suggestion")];
    [whiteBackView addSubview:suggestionText];
    
    //意见占位
    _suggestionField = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, 200, 50)];
    _suggestionField.text =@" 意见";
    _suggestionField.textColor = RGB(165, 164, 165);
    _suggestionField.leftView = [[UIImageView alloc]initWithImage:IMG(@"write")];
    _suggestionField.leftViewMode = UITextFieldViewModeAlways;
    _suggestionField.hidden = NO;
    _suggestionField.enabled = NO;
    [suggestionText addSubview:_suggestionField];
    
    //重置按钮
    UIButton *resetBtn = [[UIButton alloc]initWithFrame:CGRectMake(whiteBackView.frame.size.width/6, CGRectGetMaxY(suggestionText.frame)+paddingTop/2, 100, 48)];
    resetBtn.backgroundColor = [UIColor colorWithPatternImage:IMG(@"btnBack")];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    resetBtn.titleLabel.font = [UIFont systemFontOfSize:25];
    [resetBtn addTarget:self action:@selector(resetAct) forControlEvents:UIControlEventTouchUpInside];
    [whiteBackView addSubview:resetBtn];
    
    //提交按钮
    UIButton *submmitBtn = [[UIButton alloc]initWithFrame:CGRectMake(whiteBackView.frame.size.width/8*6-30, CGRectGetMaxY(suggestionText.frame)+paddingTop/2, 100, 48)];
    submmitBtn.backgroundColor = [UIColor colorWithPatternImage:IMG(@"btnBack")];
    [submmitBtn setTitle:@"提交" forState:UIControlStateNormal];
    submmitBtn.titleLabel.font = [UIFont systemFontOfSize:25];
    [submmitBtn addTarget:self action:@selector(submmitAct) forControlEvents:UIControlEventTouchUpInside];
    [whiteBackView addSubview:submmitBtn];
}

#pragma mark - uitextviewdelegate
-(void)textViewDidChange:(UITextView *)textView{
    _suggestionField.hidden = YES;
    
}

#pragma mark 重置按钮act
-(void)resetAct{
    _suggestionField.hidden = NO;

}


#pragma mark 提交按钮act
-(void)submmitAct{
    //判断是否为空
    if ([suggestionText.text isEqualToString:@""]) {
        _alertView = [[UIAlertView alloc]initWithTitle:nil message:@"意见不能为空！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [_alertView show];
        return;
    }else if(![userTel.text isEqualToString:@""]&&![self isTel:userTel.text]){
        _alertView = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的电话号码！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [_alertView show];
        return;
    }else if([userName.text isEqualToString:@""]||[userTel.text isEqualToString:@""]){
        _alertView = [[UIAlertView alloc]initWithTitle:nil message:@"建议输入姓名和电话，方便日后反馈信息！" delegate:nil cancelButtonTitle:@"返回填写" otherButtonTitles:@"继续提交",nil];
        _alertView.delegate = self;
        _alertView.tag = 201;
        [_alertView show];
        return;
    }
    //发送请求
    [self sendHttpRequest];
}
-(void)sendHttpRequest{
    [SVProgressHUD showWithStatus:@"正在提交.." maskType:SVProgressHUDMaskTypeBlack];
    NSString *tagStr;
    switch (type) {
        case 101:
            tagStr = @"0";
            break;
        case 102:
            tagStr = @"1";
            break;
        case 103:
            tagStr = @"2";
            break;
        default:
            break;
    }
    //发送请求
    NSURL *loginUrl = [[NSURL alloc]initWithString:HTTP_SUGGESTION];
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc]initWithURL:loginUrl];
    request.delegate = self;
    [request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] forKey:@"uid"];
    [request setPostValue:userName.text forKey:@"customer"];
    [request setPostValue:userTel.text forKey:@"phone"];
    [request setPostValue:tagStr forKey:@"tag"];
    [request setPostValue:suggestionText.text forKey:@"note"];
    [request setDidFinishSelector:@selector(httpFinish:)];
    [request setDidFailSelector:@selector(httpFail:)];
    [request startAsynchronous];
}
//成功
-(void)httpFinish:(ASIFormDataRequest *)request{
    [SVProgressHUD dismiss];
    if (![request error]) {//没有错误
        //取出data类型的数据
        NSData *jsonData = [request responseData];
        //根据utf-8编码转成字符串形式
        NSString *jsonString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
        //转成json
        NSMutableDictionary *kitData = [jsonString objectFromJSONString];
        //判断账号信息是否验证成功
        NSString *flag = [NSString stringWithFormat:@"%@",[kitData objectForKey:@"recode"]];
        NSLog(@"意见反馈%@",kitData);
        if ([flag isEqualToString:@"0"]) {//成功
            [SVProgressHUD showSuccessWithStatus:@"意见反馈成功！"];
        }else if([flag isEqualToString:@"1"]){//失败
            _alertView = [[UIAlertView alloc]initWithTitle:nil message:[kitData objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [_alertView show];
            return;
        }
    }else{
        [SVProgressHUD showErrorWithStatus:@"请检查您的网络！"];
    }
}
//失败
-(void)httpFail:(ASIFormDataRequest *)request{
    NSError *error = [request error];
    if (error.code == ASIRequestTimedOutErrorType) {
        [SVProgressHUD showErrorWithStatus:@"网络请求超时！"];
    }else if (error.code == ASIRequestCancelledErrorType){
        [SVProgressHUD showErrorWithStatus:@"网络请求失败！"];
    }
    NSLog(@"Connection faild! Error - %@ %@",[error localizedDescription],[[error userInfo]objectForKey:NSURLErrorFailingURLErrorKey]);
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 201 && buttonIndex == 1) {//继续提交请求
        [self sendHttpRequest];
    }
}
-(void)tagAct:(UIButton *)btn{
    UIButton *opinionBtn = (UIButton *)[self.view viewWithTag:101];
    UIButton *praiseBtn = (UIButton *)[self.view viewWithTag:102];
    UIButton *complaintBtn = (UIButton *)[self.view viewWithTag:103];
    if (btn.tag == 101) {//意见
        opinionBtn.selected = YES;
        praiseBtn.selected = NO;
        complaintBtn.selected = NO;
    }else if (btn.tag == 102){//表扬
        praiseBtn.selected = YES;
        opinionBtn.selected = NO;
        complaintBtn.selected = NO;
    }else{//投诉
        complaintBtn.selected = YES;
        opinionBtn.selected = NO;
        praiseBtn.selected = NO;
    }
    type = btn.tag;
}

//失去焦点
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [userName resignFirstResponder];
    [userTel resignFirstResponder];
    [suggestionText resignFirstResponder];
    if ([suggestionText.text isEqual:@""]) {//啥都没有 就再次显示占位lable
        _suggestionField.hidden = NO;
    }
}
//正则表达式匹配手机号
-(BOOL)isTel:(NSString *)tel{
    //正则里如果有\需要再加一个\进行转义
    NSString *telRegex = @"(^(\\d{3,4}-)?\\d{7,8})$|(13[0-9]{9})";
    //oc中的正则类
    NSPredicate *telPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",telRegex];
    //匹配正则表达式
    return [telPredicate evaluateWithObject:tel];
}
@end
